package com.eastfair.projectcore.controller;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.projectcore.dto.FunctionDTO;
import com.eastfair.projectcore.dto.FunctionPageQuery;
import com.eastfair.projectcore.dto.FunctionSaveDTO;
import com.eastfair.projectcore.dto.FunctionUpdateDTO;
import com.eastfair.projectcore.entity.Function;
import com.eastfair.projectcore.service.FunctionService;
import com.eastfair.projectcore.vo.FunctionVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * 功能
 * </p>
 *
 * @author ligang
 * @date 2021-07-26
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/function")
@Api(value = "Function", tags = "功能")
public class FunctionController extends SuperController<FunctionService, Long, Function, FunctionPageQuery, FunctionSaveDTO, FunctionUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Function> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Function> functionList = list.stream().map((map) -> {
            Function function = Function.builder().build();
            //TODO 请在这里完成转换
            return function;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(functionList));
    }


    /**
     * 删除
     *
     * @param id 主键id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/del")
    public R del(@RequestBody Long id) {
        return baseService.del(id);
    }

    /**
     * 创建事件
     *
     * @param functionSaveDTO 事件新增实体
     * @return
     */
    @ApiOperation(value = "创建事件", notes = "创建事件")
    @PostMapping("/createFunction")
    public R createFunction(@RequestBody FunctionSaveDTO functionSaveDTO) {
        Function function = new Function();
        BeanUtils.copyProperties(functionSaveDTO, function);
        baseService.save(function);
        return R.success(function);
    }


    /**
     * 查询事件信息
     *
     * @param functionDTO 事件实体
     * @return 查询结果
     */
    @ApiOperation(value = "查询事件信息", notes = "查询事件信息")
    @PostMapping("/findAll")
    public R<List<FunctionVO>> findAllByChildId(@RequestBody FunctionDTO functionDTO) {
        List<Long> ids = new ArrayList<>();
        if (functionDTO != null && functionDTO.getFunctionIds() != null) {
            ids = functionDTO.getFunctionIds();
        }
        return success(baseService.findAll(ids));
    }



}
